package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentJDBCDAO {
Connection connection=null;
PreparedStatement pstmt=null;
ResultSet resultSet=null;

public StudentJDBCDAO(){}

private Connection getConnection() throws SQLException{
	Connection conn;
    conn = CFactory.getingInstance().getingConnection();
     return conn;
	}

public void add(StudentBean studentBean){
	try{
		String queryString="INSERT INTO student(RollNo, Name, Course, Address) VALUES(?,?,?,?)";
		connection=getConnection();
		pstmt=connection.prepareStatement(queryString);
		pstmt.setInt(1, studentBean.getRollNo());
        pstmt.setString(2, studentBean.getName());
        pstmt.setString(3, studentBean.getCourse());
        pstmt.setString(4, studentBean.getAddresss());
        pstmt.executeUpdate();
        System.out.println("Data Added Successfully");
} catch (SQLException e) {
        e.printStackTrace();
} finally {
        try {
                if (pstmt != null)
                        pstmt.close();
                if (connection != null)
                        connection.close();
        } catch (SQLException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }

	}
}
public void update(StudentBean studentBean) {

    try {
            String queryString = "UPDATE student SET Name=? WHERE RollNo=?";
            connection = getConnection();
            pstmt = connection.prepareStatement(queryString);
            pstmt.setString(1, studentBean.getName());
            pstmt.setInt(2, studentBean.getRollNo());
            pstmt.executeUpdate();
            System.out.println("Table Updated Successfully");
    } catch (SQLException e) {
            e.printStackTrace();
    } finally {
            try {
                    if (pstmt != null)
                            pstmt.close();
                    if (connection != null)
                            connection.close();
            }

            catch (SQLException e) {
                    e.printStackTrace();
            } catch (Exception e) {
                    e.printStackTrace();

            }
    }
}

public void delete(int rollNo) {

    try {
            String queryString = "DELETE FROM student WHERE RollNo=?";
            connection = getConnection();
            pstmt = connection.prepareStatement(queryString);
            pstmt.setInt(1, rollNo);
            pstmt.executeUpdate();
            System.out.println("Data deleted Successfully");
    } catch (SQLException e) {
            e.printStackTrace();
    } finally {
            try {
                    if (pstmt != null)
                            pstmt.close();
                    if (connection != null)
                            connection.close();
            } catch (SQLException e) {
                    e.printStackTrace();
            } catch (Exception e) {
                    e.printStackTrace();
            }

    }
}


public void findAll() {
    try {
            String queryString = "SELECT * FROM student";
            connection = getConnection();
            pstmt = connection.prepareStatement(queryString);
            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                    System.out.println("Roll No " + resultSet.getInt("RollNo")
                                    + ", Name " + resultSet.getString("Name") + ", Course "
                                    + resultSet.getString("Course") + ", Address "
                                    + resultSet.getString("Address"));
            }
    } catch (SQLException e) {
            e.printStackTrace();
    } finally {
            try {
                    if (resultSet != null)
                            resultSet.close();
                    if (pstmt != null)
                            pstmt.close();
                    if (connection != null)
                            connection.close();
            } catch (SQLException e) {
                    e.printStackTrace();
            } catch (Exception e) {
                    e.printStackTrace();
            }

    }

}
}